package com.xxg.fileshand.activity

import android.content.Intent
import android.os.Bundle
import com.blankj.utilcode.util.SPUtils
import com.xxg.fileshand.adapter.OperateTypeAdapter
import com.xxg.fileshand.app.App
import com.xxg.fileshand.base.BaseVMActivity
import com.xxg.fileshand.base.NoViewModel
import com.xxg.fileshand.constant.Constant
import com.xxg.fileshand.databinding.ActivityHomeBinding
import com.xxg.fileshand.reader.ReaderUtils
import com.xxg.fileshand.type.OperateType
import com.xxg.fileshand.type.OperateType.*

class HomeActivity : BaseVMActivity<ActivityHomeBinding, NoViewModel>() {

    var operateTypeAdapter: OperateTypeAdapter? = null
    var operateTypeList: MutableList<OperateType> = mutableListOf()

    var isInOut: Boolean? = null
    override fun getMyTitle(): String {
        return "管理中心"
    }

    override fun onRightClick() {
        startActivity(SettingActivity::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        showLeftText("")
        showRightText("设置")
        mBinding.tvUser.text = "欢迎您：${App.userName}"
        operateTypeAdapter = OperateTypeAdapter()
        mBinding.rvList.adapter = operateTypeAdapter
        isInOut = SPUtils.getInstance().getBoolean(Constant.IS_IN_OUT)
        //operateTypeList中添加OperateType对象
        operateTypeList.addAll(OperateType.values().toMutableList().filter {
//            if (SetInfo.getFileMode() == 1) { //当是图书模式时 过滤掉盘点和查询
//                it.type != OPERATE_TYPE_BORROW.type && it.type != OPERATE_TYPE_RETURN.type &&
            if (!SPUtils.getInstance().getBoolean(Constant.IS_IN_OUT)) {
                it.type != OPERATE_TYPE_RETURN.type && it.type != OPERATE_TYPE_BORROW.type
            } else {
                true
            }
        }.filter {
            if (!SPUtils.getInstance().getBoolean(Constant.IS_GUIDE)) {
                it.type != OPERATE_TYPE_GUIDE.type
            } else {
                true
            }
        }.filter {
            if (!SPUtils.getInstance().getBoolean(Constant.IS_FIND)) {
                it.type != OPERATE_TYPE_SEARCH.type
            } else {
                true
            }
        }.filter {
            if (!SPUtils.getInstance().getBoolean(Constant.IS_REVERSE)) {
                it.type != OPERATE_TYPE_REVERSE.type
            } else {
                true
            }
        })
        operateTypeAdapter?.setNewInstance(operateTypeList)
        operateTypeAdapter?.setOnItemClickListener { adapter, view, position ->
            if (ReaderUtils.instance.isInit == null) {
                toast("RFID正在初始化...")
                return@setOnItemClickListener
            }
            if (ReaderUtils.instance.isInit == false) {
                toast("RFID初始化失败，请重启App或设备")
//                return@setOnItemClickListener
            }
            when (operateTypeAdapter!!.data[position].type) {
                0 -> {
                    startActivity(GoodsInventoryActivity::class.java)
                }

                1, 2, 3, 4, 5, 6 -> {
                    // 上架指导、档案上架、档案下架、档案入库、档案出库、档案倒架
                    val intent = Intent(this, FilesInOutActivity::class.java)
                    intent.putExtra("operateType", operateTypeList[position])
                    startActivity(intent)
                }
//                6 -> {
//                    // 档案倒架
//                }
                7 -> {
                    // 档案查询
                    startActivity(FilesFindActivity::class.java)
                }

                8 -> {
                    // 标签测试
                    startActivity(TagCheckActivity::class.java)
                }
            }
        }

        ReaderUtils.instance.initReader(this)

    }

    override fun onBackPressed() {
//        super.onBackPressed()
    }

}